<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="600px"
      @closed="resetForm('form')"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-form-item label="资源名称" prop="resourceList">
            <el-select
              v-model="checkedList"
              multiple
              placeholder="请选择资源名称"
              @change="handlerChang"
              style="width: 100%"
            >
              <el-option
                v-for="v in courseData"
                :key="v.id"
                :label="v.title"
                :value="v.id"
              >
                <span style="float: left">{{ v.title }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{
                  showTeam(v)
                }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { expertsReviewResourceAdd } from '@/api/admin/audit/expertsReviewResource.js'
import { sysCourseQuery } from '@/api/admin/courseLibrary/sysCourse.js'
import Bus from '@/utils/bus.js'
export default {
  data() {
    return {
      title: '新增',
      dialogVisible: false,
      addOrEdit: 1,
      checkedData: {},
      form: {
        resType: '1',
        reviewId: '', //评审会
        resourceList: [], //资源集合
      },
      rules: {
        resourceList: [
          { required: true, message: '请选择资源', trigger: 'blur' },
        ],
      },
      extFiled: {},
      courseData: [],
      checkedList: [],
    }
  },
  props: ['reviewData'],
  computed: {
    showTeam() {
      return function (v) {
        return v.sysCourseTeamList
          .filter((m) => m.userId == m.courseOriginator)
          .map((m) => m.userName)
          .toString()
      }
    },
  },
  watch: {
    reviewData: {
      immediate: true,
      handler() {
        this.extFiled = JSON.parse(this.reviewData.extFiled)
        this.getCourseList()
      },
    },
  },
  mounted() {
    Bus.$on('showDialogReviewResourceAdd', this.init)
  },
  beforeDestroy() {
    Bus.$off('showDialogReviewResourceAdd')
  },
  methods: {
    //显示弹窗
    init(row) {
      if (row) {
        this.form = { ...this.form, ...row }
        this.form.date = [this.form.startTime, this.form.endTime]
        this.$set(this.form, 'id', row.id)
      } else {
        this.form = this.$options.data().form
        this.$delete(this.form, 'id')
      }
      this.title = row ? '编辑' : '新增'
      this.dialogVisible = true
    },
    submitForm(form) {
      this.form.reviewId = this.reviewData.id
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.add()
        }
      })
    },
    getCourseList() {
      let data = {
        page: 1,
        limit: 100,
        courseType: this.extFiled.courseType,
        // status: "", //课程状态：0-未发布，1-已发布
        // auditStatus: "",
      }
      sysCourseQuery(data).then((res) => {
        this.courseData = res.data.data
      })
    },
    handlerChang(v) {
      this.form.resourceList = this.courseData.filter(
        (v) => this.checkedList.indexOf(v.id) != -1,
      )
    },
    //新增
    add() {
      expertsReviewResourceAdd(this.form).then((res) => {
        this.done(res)
      })
    },
    done(res) {
      if (!res || res.data.code != 0) return
      this.dialogVisible = false
      Bus.$emit('refreshReviewResource')
      this.$message({
        message: '操作成功',
        type: 'success',
      })
    },
    handleSelect() {
      Bus.$emit('userSelect')
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields()
      this.checkedList = []
    },
  },
}
</script>
<style scoped>
.user-wrap {
  display: inline-block;
  width: 560px;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
  overflow: hidden;
  min-height: 40px;
  cursor: not-allowed;
}
.user-wrap p {
  line-height: 40px;
  color: #bbb;
  text-indent: 10px;
}
.user-wrap span {
  margin: 0 5px;
}
.user-select-button {
  float: right;
  padding-right: 10px;
  cursor: pointer;
}
.user-select-button span {
  font-size: 28px;
  color: #930b0b;
}
</style>